package com.mlnx.yingbaisui.device.utils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * 睡眠检测仪sdk
 */
public class HealthCare extends CommonDevice {

    //========================55aa01 查下产品信息========================

    public static HashMap<String, Object> receiveQueryProduct(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("deviceType", Integer.parseInt(data.substring(12, 14), 16));
        resultMap.put("version", Integer.parseInt(data.substring(14, 18), 16));
        resultMap.put("reportSwitch", Integer.parseInt(data.substring(18, 20), 16));
        resultMap.put("reportGenerateTime-h", Integer.parseInt(data.substring(20, 22), 16));
        resultMap.put("reportGenerateTime-m", Integer.parseInt(data.substring(22, 24), 16));
        resultMap.put("reportGenerateTime-s", Integer.parseInt(data.substring(24, 26), 16));
        resultMap.put("maxDistance", Integer.parseInt(data.substring(26, 30), 16));
        resultMap.put("minDistance", Integer.parseInt(data.substring(30, 34), 16));
        resultMap.put("sleepAnalysisStartTime-h", Integer.parseInt(data.substring(34, 36), 16));
        resultMap.put("sleepAnalysisStartTime-m", Integer.parseInt(data.substring(36, 38), 16));
        resultMap.put("sleepAnalysisStartTime-s", Integer.parseInt(data.substring(38, 40), 16));
        resultMap.put("sleepAnalysisEndTime-h", Integer.parseInt(data.substring(40, 42), 16));
        resultMap.put("sleepAnalysisEndTime-m", Integer.parseInt(data.substring(42, 44), 16));
        resultMap.put("sleepAnalysisEndTime-s", Integer.parseInt(data.substring(44, 46), 16));
        resultMap.put("unmannedEscalation", Integer.parseInt(data.substring(46, 48), 16));
        resultMap.put("offlineEscalationFrequency", Integer.parseInt(data.substring(48, 50), 16));
        resultMap.put("onlineEscalationFrequency", Integer.parseInt(data.substring(50, 52), 16));
//        resultMap.put("sleepTimeDeviation", Integer.parseInt(data.substring(52, 54), 16));
//        resultMap.put("sleepTimeDeviationValue", Integer.parseInt(data.substring(54, 58), 16));
//        resultMap.put("breathAbnormalLevel", Integer.parseInt(data.substring(58, 60), 16));
//        resultMap.put("aiModel", Integer.parseInt(data.substring(60, 62), 16));
//        resultMap.put("aiMin", Integer.parseInt(data.substring(62, 64), 16));
//        resultMap.put("aiMax", Integer.parseInt(data.substring(64, 66), 16));
//        resultMap.put("aiAvg", Integer.parseInt(data.substring(66, 68), 16));
//        resultMap.put("aiRisk", Integer.parseInt(data.substring(68, 70), 16));
//        resultMap.put("signAbnormalParam1", Integer.parseInt(data.substring(70, 72), 16));
//        resultMap.put("signAbnormalParam2", Integer.parseInt(data.substring(72, 74), 16));
//        resultMap.put("signAbnormalParam3", Integer.parseInt(data.substring(74, 76), 16));
//        resultMap.put("interfere", Integer.parseInt(data.substring(76, 78), 16));
//        resultMap.put("breathTooHigh", Integer.parseInt(data.substring(78, 82), 16));
//        resultMap.put("breathTooLow", Integer.parseInt(data.substring(82, 86), 16));
        return resultMap;
    }

    //========================55aa30 监测数据上报========================

    public static HashMap<String, Object> receiveDetectionResult(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("heartRate", Integer.parseInt(data.substring(12, 14), 16));
        resultMap.put("respiratoryRate", Integer.parseInt(data.substring(14, 16), 16));
        resultMap.put("bodyExistStatus", Integer.parseInt(data.substring(16, 18), 16));
        resultMap.put("bodyStatus", Integer.parseInt(data.substring(18, 20), 16));
        resultMap.put("minDistanceTarget", Integer.parseInt(data.substring(20, 24), 16));
        resultMap.put("signExceptionWarn", Integer.parseInt(data.substring(24, 26), 16));
        //把16进制转浮点类型 并保留2为小数
        resultMap.put("signalStrength", String.format("%.2f", Float.intBitsToFloat(new BigInteger(data.substring(26, 34), 16).intValue())));
        resultMap.put("inTime", Integer.parseInt(data.substring(34, 38), 16));
        resultMap.put("offTime", Integer.parseInt(data.substring(38, 42),16));
        resultMap.put("time-h", Integer.parseInt(data.substring(48, 50),16));
        resultMap.put("time-m", Integer.parseInt(data.substring(50, 52),16));
        resultMap.put("time-s", Integer.parseInt(data.substring(52, 54),16));
        return resultMap;
    }

    //========================55aa31 最大距离设置========================

    public static String sendMaxDistance(int distance) {
        String first = StringUtils.toHexStr(distance / 256);
        String second = StringUtils.toHexStr(distance % 256);
        String tmp = "55aa3102"+ first + second;
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveMaxDistance(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("maxDistance", Integer.parseInt(data.substring(12), 16));
        return resultMap;
    }

    //========================55aa32 最小距离设置========================

    public static String sendMinDistance(int distance) {
        String first = StringUtils.toHexStr(distance / 256);
        String second = StringUtils.toHexStr(distance % 256);
        String tmp = "55aa3202"+ first + second;
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveMinDistance(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("minDistance", Integer.parseInt(data.substring(12), 16));
        return resultMap;
    }

    //========================55aa33 危险预警设置========================

    public static String sendBreathAbnormalParam(int level, int onOff, int avgValue, int minValue, int maxValue, int risk, int tooHigh, int tooLow) {
        String tooHighFirst = StringUtils.toHexStr(tooHigh / 256);
        String tooHighSecond = StringUtils.toHexStr(tooHigh % 256);
        String tooLowFirst = StringUtils.toHexStr(tooLow / 256);
        String tooLowSecond = StringUtils.toHexStr(tooLow % 256);
        String tmp = "55aa330b01"+ StringUtils.toHexStr(level) + StringUtils.toHexStr(onOff) +
                StringUtils.toHexStr(avgValue) + StringUtils.toHexStr(minValue) + StringUtils.toHexStr(maxValue) +
                StringUtils.toHexStr(risk) + tooHighFirst
                + tooHighSecond + tooLowFirst + tooLowSecond;
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static String sendSignAbnormalParam(int param1, int param2, int param3, int interfere, int tooHigh, int tooLow) {
        String tooHighFirst = StringUtils.toHexStr(tooHigh / 256);
        String tooHighSecond = StringUtils.toHexStr(tooHigh % 256);
        String tooLowFirst = StringUtils.toHexStr(tooLow / 256);
        String tooLowSecond = StringUtils.toHexStr(tooLow % 256);
        String tmp = "55aa330b02"+ StringUtils.toHexStr(param1) + StringUtils.toHexStr(param2) +
                StringUtils.toHexStr(param3) + StringUtils.toHexStr(interfere) + tooHighFirst
                + tooHighSecond + tooLowFirst + tooLowSecond + "0000";
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveHazardWarning(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("type", Integer.parseInt(data.substring(12,14), 16));
        resultMap.put("param1", Integer.parseInt(data.substring(14,16), 16));
        resultMap.put("param2", Integer.parseInt(data.substring(16,18), 16));
        resultMap.put("param3", Integer.parseInt(data.substring(18,20), 16));
        resultMap.put("param4", Integer.parseInt(data.substring(20,22), 16));
        resultMap.put("param5", Integer.parseInt(data.substring(22,24), 16));
        resultMap.put("param6", Integer.parseInt(data.substring(24,26), 16));
        resultMap.put("breathTooHighSum", Integer.parseInt(data.substring(26,30), 16));
        resultMap.put("breathTooLowSum", Integer.parseInt(data.substring(30,34), 16));
        return resultMap;
    }

    //========================55aa60 睡眠报告开关设置========================

    public static String sendOnOff(int status) {
        String tmp = status == 1 ? "55aa600101" : "55aa600100";
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveSleepSwitch(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("sleepSwitch", Integer.parseInt(data.substring(12), 16) % 256);
        return resultMap;
    }

    //========================55aa62 睡眠报告结果上报========================

    public static String sendQueryReportResult() {
        String tmp = "55aa6200";
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveReportGenerateResult(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        int resultInt = Integer.parseInt(data.substring(12,14), 16);
        resultMap.put("generateResult", resultInt > 127 ?  resultInt - 256 : resultInt);
        int pack = Integer.parseInt(data.substring(14,16), 16);
        resultMap.put("pack", pack);
        return resultMap;
    }

    //========================55aa61 睡眠报告生成时间设置========================

    public static String sendSleepReportTime(String str) {
        String[] time = str.split(":");
        String hh = StringUtils.toHexStr(Integer.valueOf(time[0]));
        String mm = StringUtils.toHexStr(Integer.valueOf(time[1]));
        String ss = StringUtils.toHexStr(Integer.valueOf(time[2]));
        String tmp = "55aa6103" + hh + mm + ss;
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveSleepReportTime(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("reportGenerateTime-h", Integer.parseInt(data.substring(12, 14), 16));
        resultMap.put("reportGenerateTime-m", Integer.parseInt(data.substring(14, 16), 16));
        resultMap.put("reportGenerateTime-s", Integer.parseInt(data.substring(16, 18), 16));
        return resultMap;
    }

    //========================55aa63 睡眠报告基础数据上报========================

    public static String sendSleepAnalysisResult() {
        String tmp = "55aa6300";
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveSleepQualityAnalysis(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("sleepDate-year", Integer.parseInt(data.substring(12, 14), 16) + 2000);
        resultMap.put("sleepDate-month", Integer.parseInt(data.substring(14, 16), 16));
        resultMap.put("sleepDate-day", Integer.parseInt(data.substring(16, 18), 16));
        resultMap.put("sleepTime-h", Integer.parseInt(data.substring(18, 20), 16));
        resultMap.put("sleepTime-m", Integer.parseInt(data.substring(20, 22), 16));
        resultMap.put("sleepTime-s", Integer.parseInt(data.substring(22, 24), 16));
        resultMap.put("soberTime-h", Integer.parseInt(data.substring(24, 26), 16));
        resultMap.put("soberTime-m", Integer.parseInt(data.substring(26, 28), 16));
        resultMap.put("soberTime-s", Integer.parseInt(data.substring(28, 30), 16));
        resultMap.put("eyeMoveDuration", Integer.parseInt(data.substring(30, 32), 16) + Integer.parseInt(data.substring(32, 34), 16) * 256);
        resultMap.put("eyeMove", Integer.parseInt(data.substring(34, 36), 16) + "." + Integer.parseInt(data.substring(36, 38), 16));
        resultMap.put("LightSleepDuration", Integer.parseInt(data.substring(38, 40), 16) + Integer.parseInt(data.substring(40, 42), 16) * 256);
        resultMap.put("LightSleep", Integer.parseInt(data.substring(42, 44), 16) + "." + Integer.parseInt(data.substring(44, 46), 16));
        resultMap.put("deepSleepDuration", Integer.parseInt(data.substring(46, 48), 16) + Integer.parseInt(data.substring(48, 50), 16) * 256);
        resultMap.put("deepSleep", Integer.parseInt(data.substring(50, 52), 16) + "." + Integer.parseInt(data.substring(52, 54), 16));
        resultMap.put("soberDuration", Integer.parseInt(data.substring(54, 56), 16) + Integer.parseInt(data.substring(56, 58), 16) * 256);
        resultMap.put("sporadicNapsDuration", Integer.parseInt(data.substring(58, 60), 16) + Integer.parseInt(data.substring(60, 62), 16) * 256);
        resultMap.put("effectiveSleepDuration", Integer.parseInt(data.substring(62, 64), 16) + Integer.parseInt(data.substring(64, 66), 16) * 256 + Integer.parseInt(data.substring(66, 68), 16) * 256 * 256 );
        resultMap.put("sleepEfficiency", Integer.parseInt(data.substring(68, 70), 16) + "." + Integer.parseInt(data.substring(70, 72), 16));
        resultMap.put("LatentDuration", Integer.parseInt(data.substring(72, 74), 16) + Integer.parseInt(data.substring(74, 76), 16) * 256);
        resultMap.put("soberSum", Integer.parseInt(data.substring(76, 78), 16));
        resultMap.put("respiratoryQuality", Integer.parseInt(data.substring(78, 80), 16));
        resultMap.put("respiratoryExceptionSum", Integer.parseInt(data.substring(80, 82), 16));
        resultMap.put("maxHeartRate", Integer.parseInt(data.substring(82, 84), 16));
        resultMap.put("minHeartRate", Integer.parseInt(data.substring(84, 86), 16));
        resultMap.put("avgHeartRate", Integer.parseInt(data.substring(86, 88), 16));
        resultMap.put("maxRespiratoryRate", Integer.parseInt(data.substring(88, 90), 16));
        resultMap.put("minRespiratoryRate", Integer.parseInt(data.substring(90, 92), 16));
        resultMap.put("avgRespiratoryRate", Integer.parseInt(data.substring(92, 94), 16));
        resultMap.put("sleepQualityScore", Integer.parseInt(data.substring(94, 96), 16));
        resultMap.put("turnSum", Integer.parseInt(data.substring(96, 98), 16));
        resultMap.put("sleepFornt", Integer.parseInt(data.substring(98, 100), 16));
        resultMap.put("sleepMiddle", Integer.parseInt(data.substring(100, 102), 16));
        resultMap.put("sleepLast", Integer.parseInt(data.substring(102, 104), 16));
        resultMap.put("sleepWeak", Integer.parseInt(data.substring(104, 106), 16));
        return resultMap;
    }

    //========================55aa64-55aa69 睡眠报告 睡眠质量分析========================

    public static String sendSleepData() {
        String tmp = "55aa6400";
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveSleepDataBySum(int len, String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        ArrayList<Object> resultList = new ArrayList<>();
        for (int i = 12; i < len * 2; i += 2) {
            resultList.add(Integer.parseInt(data.substring(i, i+2), 16));
        }
        resultMap.put("sleepTypeList", resultList);
        return resultMap;
    }

    //========================55aa74-55aa79 睡眠报告 呼吸数据========================

    public static String sendSleepBreathData() {
        String tmp = "55aa7400";
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveBreathDataBySum(int len, String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        ArrayList<Object> resultList = new ArrayList<>();
        for (int i = 12; i < len * 2; i += 2) {
            resultList.add(Integer.parseInt(data.substring(i, i+2), 16));
        }
        resultMap.put("breathDataList", resultList);
        return resultMap;
    }

    //========================55aa7a-55aa7f 睡眠报告 心率数据========================

    public static String sendSleepHeartData() {
        String tmp = "55aa7a00";
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveHeartDataBySum(int len, String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        ArrayList<Object> resultList = new ArrayList<>();
        for (int i = 12; i < len * 2; i += 2) {
            resultList.add(Integer.parseInt(data.substring(i, i+2), 16));
        }
        resultMap.put("heartDataList", resultList);
        return resultMap;
    }

    //========================55aa6a 睡眠报告 分析时间段========================

    /**
     * 设置睡眠分析时间
     * @return
     */
    public static String sendSleepAnalysisTime(int startH, int startM, int startS, int endH, int endM, int endS) {
        String tmp = "55aa6a06" + StringUtils.toHexStr(startH) + StringUtils.toHexStr(startM) + StringUtils.toHexStr(startS)
                + StringUtils.toHexStr(endH) + StringUtils.toHexStr(endM) + StringUtils.toHexStr(endS);
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveSleepAnalysisTime(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("result", Integer.parseInt(data.substring(12, 14), 16));
        resultMap.put("sleepAnalysisStartTime-h", Integer.parseInt(data.substring(14, 16), 16));
        resultMap.put("sleepAnalysisStartTime-m", Integer.parseInt(data.substring(16, 18), 16));
        resultMap.put("sleepAnalysisStartTime-s", Integer.parseInt(data.substring(18, 20), 16));
        resultMap.put("sleepAnalysisEndTime-h", Integer.parseInt(data.substring(20, 22), 16));
        resultMap.put("sleepAnalysisEndTime-m", Integer.parseInt(data.substring(22, 24), 16));
        resultMap.put("sleepAnalysisEndTime-s", Integer.parseInt(data.substring(24, 26), 16));
        return resultMap;
    }

}
